package com.chen.controller;

import com.alibaba.fastjson2.JSONObject;
import com.chen.core.domain.R;
import com.chen.domain.Document;
import com.chen.entity.DateEntity;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@RestController
public class KafkaController {

    @Autowired
    private KafkaTemplate<String, Object> kafkaTemplate;

    @GetMapping("/kafka/normal/{message}")
    public void sendMessage1(@PathVariable("message") String normalMessage) {
        kafkaTemplate.send("topic1", normalMessage);
    }

    @GetMapping("/date")
    public R<?> sendMessage1(@JsonFormat(pattern = "yyyy-MM-dd") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date date) {
        return R.ok(date != null ? date : new Date());
    }

    @GetMapping("/localDate")
    public R<?> sendMessage1(@JsonFormat(pattern = "yyyy-MM-dd") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime date) {
        return R.ok(date != null ? date : LocalDateTime.now());
    }

    @GetMapping("/dateEntity")
    public R<?> sendMessage1(DateEntity dateEntity) {
        return R.ok(dateEntity);
    }

    @PostMapping("/json")
    public R<?> sendMessage1(@RequestBody JSONObject json) {
        return R.ok(json);
    }

    @PostMapping("/document")
    public R<?> setKafkaTemplate(List<Document> documentList) {
        return R.ok(documentList);
    }
}
